package com.clkj.zhonghui.controller.product;

import com.clkj.zhonghui.pojo.common.OSSUtil;
import com.clkj.zhonghui.pojo.common.Result;
import com.clkj.zhonghui.pojo.common.StatusCode;
import com.clkj.zhonghui.pojo.product.Category;
import com.clkj.zhonghui.service.product.CategoryService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author：周佳润
 * @Date：2024/7/11 上午9:18
 */
@RestController
@RequestMapping(value = "/Category")
@Api(value = "/Category", tags = {"商品分类"})
@CrossOrigin
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @PostMapping("/addCategory")
    @ApiOperation(value = "添加新商品分类")
    public Result addCategory(Category category,
                              @ApiParam(name = "flag", value = "添加 add，修改 edit") @RequestParam("flag") String  flag,
                              HttpServletRequest request){
        try {
            categoryService.addCategory(category,flag,request);
            return new Result(true, StatusCode.OK, "添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "添加失败");
        }
    }

    @PostMapping("/listCategory")
    @ApiOperation(value = "获取商品分类")
    public Result listCategory(
                               @ApiParam(name = "shopId", value = "门店id，当id为-1时查询全部") @RequestParam("shopId")String shopId){
        try {
            List<Category> categoryList = categoryService.listCategory(shopId);
            return new Result(true, StatusCode.OK, "查询成功",categoryList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "查询失败");
        }
    }

    @PostMapping("/updateCategory")
    @ApiOperation(value = "更新商品分类状态")
    public Result updateCategory(@ApiParam(name = "cateIds", value = "轮播id集合字符串") @RequestParam("cateIds") String cateIds,
                                 @ApiParam(name = "categoryStatus", value = "状态") @RequestParam("categoryStatus") Integer categoryStatus){
        try {
            categoryService.updateCategory(cateIds, categoryStatus);
            return new Result(true, StatusCode.OK, "更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "更新失败");
        }
    }

    @PostMapping("/delCategory")
    @ApiOperation(value = "删除商品分类")
    public Result delCategory(@ApiParam(name = "imgIds", value = "分类id集合") @RequestParam("imgIds") String imgIds){
        try {
            categoryService.delCategory(imgIds);
            return new Result(true, StatusCode.OK, "更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "更新失败");
        }
    }
}
